# PyMusique
# (C) Zee Zuo 2010

import pysynth
import math
from random import *
import os

from Tkinter import *
from Dialog import *
import tkMessageBox

chain = {
		"c": [(28, "c"), (48, "d"), (64, "e"), (78, "f"), (89, "g"), (96, "a"), (99, "b"), (100, "c5")],
		"d": [(20, "c"), (41, "d"), (61, "e"), (76, "f"), (87, "g"), (94, "a"), (98, "b"), (100, "c5")],
		"e": [(14, "c"), (32, "d"), (51, "e"), (69, "f"), (83, "g"), (92, "a"), (97, "b"), (100, "c5")],
		"f": [(9, "c"), (22, "d"), (38, "e"), (56, "f"), (72, "g"), (85, "a"), (94, "b"), (100, "c5")],
		"g": [(5, "c"), (14, "d"), (27, "e"), (43, "f"), (61, "g"), (77, "a"), (90, "b"), (100, "c5")],
		"a": [(3, "c"), (9, "d"), (18, "e"), (32, "f"), (50, "g"), (69, "a"), (87, "b"), (100, "c5")],
		"b": [(1, "c"), (4, "d"), (11, "e"), (22, "f"), (39, "g"), (60, "a"), (82, "b"), (100, "c5")],
		"c5": [(1, "c"), (3, "d"), (7, "e"), (16, "f"), (31, "g"), (53, "a"), (80, "b"), (100, "c5")],
	}

def betterone():
	d = Dialog(None,
			title="Message",
			text="Which one is better?",
			default=0,
			bitmap=DIALOG_ICON,
			strings=("First", "Second"))
	return d.num

def synthmusic(noteslist, filename="default.wav"):
	pysynth.make_wav(noteslist, fn = filename)

def playmusic(filename="default.wav"):
	os.system("afplay " + filename)

def genmusic(numNotes):
	times = [4]
	notes = ['c','d','e','f','g','a','b','c5']

	testlist = []
	for i in range(0, numNotes):
		testlist.append((choice(notes), choice(times)))

	test = tuple(testlist)
	print test
	return test

def getnextnote(thisnote):
	print "thisnote is " + thisnote + "."
	probtbl = chain[thisnote]
	code = randint(1, 100)
	for i in probtbl:
		if code < i[0]:
			return i[1]

def getnextmusic(thisseq):
	times = [4]
	notes = ['c','d','e','f','g','a','b','c5']

	testlist = []
	currentnote = thisseq[0][0] 
	for note in thisseq:
		if randint(1, 100) > 80:
			currentnote = getnextnote(note[0])
			newtime = choice(times)
		else:
			currentnote = getnextnote(currentnote)
			newtime = note[1]
		testlist.append((currentnote, newtime))
	test = tuple(testlist)
	print test
	return test

def mainproc():
	n=10 # Magic number

	root.withdraw()
	music = [] 

	music.append(genmusic(n))
	synthmusic(music[0])
	tkMessageBox.showinfo("Message", "We are going to play music segment 1.")
	playmusic()

	music.append(genmusic(n))
	synthmusic(music[1])
	tkMessageBox.showinfo("Message", "We are going to play music segment 2.")
	playmusic()

	pref = betterone()

	if tkMessageBox.askyesno("Message", "Continue?"):
		nextround = 1
	else:
		nextround = 0

	while nextround is 1:
		nextmusic = []

		nextmusic.append(getnextmusic(music[pref]))
		synthmusic(nextmusic[0])
		tkMessageBox.showinfo("Message", "We are going to play music segment 1.")
		playmusic()

		nextmusic.append(getnextmusic(music[pref]))
		synthmusic(nextmusic[1])
		tkMessageBox.showinfo("Message", "We are going to play music segment 2.")
		playmusic()

		pref = betterone()
		music = nextmusic
	
		if tkMessageBox.askyesno("Message", "Continue?"):
			nextround = 1
		else:
			synthmusic(music[pref])
			nextround = 0
	
	exit()

root = Tk()
mainproc()
root.mainloop()
